package com.speed.controller;


import com.alibaba.fastjson.JSONObject;
import com.speed.params.ParamsEngine;
import com.speed.service.SpeedDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;


import java.util.HashMap;
import java.util.Map;

/**
 * 系统设置请求拦截
 */
@Controller
public class ManageController {
    @Autowired
    private SpeedDBService SpeedDBService;

    @RequestMapping(value = {"/"}, method = RequestMethod.GET)
    public String start() {
        return "/manage";
    }

    @RequestMapping(value = {"/setting"}, method = RequestMethod.GET)
    public String setting() {
        return "/manage";
    }

    @RequestMapping(value = "testDB", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String testDB(@RequestParam("mysqlUrl") String mysqlUrl, @RequestParam("mysqlName") String mysqlName,
                         @RequestParam("mysqlPassword") String mysqlPassword, @RequestParam("mysqlUser") String mysqlUser) {
        String url = "jdbc:mysql://" + mysqlUrl + "/" + mysqlName + "?useUnicode=true&amp;characterEncoding=utf8";
        JSONObject result = new JSONObject();
        int connection = SpeedDBService.connectDB(mysqlUrl, mysqlName, mysqlUser, mysqlPassword);
        result.put("connection", connection);
        switch (connection) {
            case -1: {
                break;
            }
            case 1: {
                result.put("result", "success");
                return result.toString();
            }
            case 2: {
                result.put("result", "created");
                result.put("message", "创建数据库成功，请保存配置后重启服务生效");
                return result.toString();
            }
        }
        result.put("result", "failure");
        result.put("message", "数据库连接失败");
        return result.toString();
    }



    /**
     * 保存本地配置
     * @param mysqlUrl
     * @return
     */
    @RequestMapping(value = {"/saveConfig"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String saveConfig(@RequestParam("mysqlUrl") String mysqlUrl, @RequestParam("mysqlName") String mysqlName,
                             @RequestParam("mysqlPassword") String mysqlPassword, @RequestParam("mysqlUser") String mysqlUser) {
        //测试数据库连接
        JSONObject result =new JSONObject();
        //保存配置
        Map<String,String> params = new HashMap<String, String>();
        params.put("jdbc.mysql.url",mysqlUrl);
        params.put("jdbc.mysql.name",mysqlName);
        params.put("jdbc.mysql.username",mysqlUser);
        params.put("jdbc.mysql.password",mysqlPassword);
        ParamsEngine.saveProperty(params);

        result.put("result", "success");
        return result.toString();
    }

    /**
     * 加载本地配置
     *
     * @return
     */
    @RequestMapping(value = {"/loadConfig"}, method = RequestMethod.GET)
    @ResponseBody
    public String loadConfig() {
        JSONObject obj = new JSONObject();
        obj.put("mysqlUrl", ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.url"));
        obj.put("mysqlName", ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.name"));
        obj.put("mysqlUser", ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.username"));
        obj.put("mysqlPassword", ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.password"));
        obj.put("data", "success");
        return obj.toString();
    }

}
